package desmontec.aluguel

import desmontec.util.Constantes;

class ConsumoAluguel {
	
	Date dia
	Double horasTrabalhadas = 0
	
	Double consumoDeDiesel = 0
	Double consumoDeOleo40 = 0
	Double consumoDeOleo68 = 0
	Double consumoDeOleo90 = 0
	Double consumoGraxas = 0
	
	Double valorGasto = 0
		
	static belongsTo = [aluguel: AluguelMaquina]

    static constraints = {
		dia(validator: {val, obj -> if (!(val.after(obj.aluguel.getInicioValidade()) && val.before(obj.aluguel.getFimValidade()) && obj.ehDiaValido())){ return['desmontec.aluguel.ConsumoAluguel.dia.erro']}})
		
		horasTrabalhadas(min: 0d , nullable:false)		
		consumoDeDiesel (min: 0d , nullable:false)
		consumoDeOleo40 (min: 0d , nullable:false)
		consumoDeOleo68 (min: 0d , nullable:false)
		consumoDeOleo90 (min: 0d , nullable:false)
		consumoGraxas 	(min: 0d , nullable:false)
		valorGasto 		(min: 0d , nullable:false)
	}
	
	boolean ehDiaValido(){
		// acha todos os consumos daquele alguel com mesma data
		List<ConsumoAluguel> consumosMesmoDia = ConsumoAluguel.findAll("FROM ConsumoAluguel ca WHERE ca.aluguel = ? and ca.dia = ?", [this.aluguel, this.dia])
		if(consumosMesmoDia != null){
			return consumosMesmoDia.isEmpty();
		}
		return true;
	}
	
	public String toString(){
		StringBuffer str = new StringBuffer();
		str.append(dia.date);
		str.append(Constantes.HIFEM);
		str.append(Constantes.CALENDARIO_MESES[dia.month]);
		str.append(Constantes.HIFEM);
		str.append(Constantes.ANO_BASE + dia.year);
		
		return str.toString();
	}
}
